package com.atwork.wuhua.utils.UpdataAppUtil;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.support.v4.content.FileProvider;

import com.atwork.wuhua.BuildConfig;
import com.atwork.wuhua.R;
import com.atwork.wuhua.utils.LogUtils;

import java.io.File;

public class NotificationUtils {
    private static NotificationUtils notificationUtils;
    private Context context;
    private NotificationManager updateNotificationManager;
    private Notification updateNotification;

    public static NotificationUtils getInstance() {
        if (notificationUtils == null) {
            notificationUtils = new NotificationUtils();
        }
        return notificationUtils;
    }

    public void init(Context context) {
        this.context = context;
    }

    public void startDownLoad() {
        Intent nullIntent = new Intent();
        PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, nullIntent, 0);
        updateNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);

        Notification.Builder builder = new Notification.Builder(context);
        builder.setContentIntent(pendingIntent);
        builder.setSmallIcon(R.mipmap.ic_launcher);// 设置图标
        builder.setWhen(System.currentTimeMillis());// 设置通知来到的时间
        builder.setContentTitle("正在更新 众屏");// 设置通知的标题
        builder.setContentText("正在下载 众屏\",\"0%");// 设置通知的内容
        builder.setTicker("正在更新 众屏");// 状态栏上显示
        builder.setOngoing(true);
        updateNotification = builder.build();

        updateNotification.defaults = Notification.DEFAULT_LIGHTS;
        updateNotification.flags = Notification.FLAG_AUTO_CANCEL;
        updateNotificationManager.notify(101, updateNotification);
    }

    public void isDownLoading(long totalSize, long updateTotalSize) {
        Intent nullIntent = new Intent();
        PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, nullIntent, 0);

        Notification.Builder builder = new Notification.Builder(context);
        builder.setContentIntent(pendingIntent);
        builder.setSmallIcon(R.mipmap.ic_launcher);// 设置图标
        builder.setWhen(System.currentTimeMillis());// 设置通知来到的时间
        builder.setContentTitle("正在更新 众屏");// 设置通知的标题
        builder.setContentText(((int) updateTotalSize * 100 / totalSize + "%").replace("-", ""));// 设置通知的内容
        builder.setTicker("正在更新 众屏");// 状态栏上显示
        builder.setOngoing(true);
        updateNotification = builder.build();

        updateNotificationManager.notify(101, updateNotification);
    }

    public void downloadSuccess(String filePath) {
        LogUtils.e("filePath==>"+filePath);

        Intent installIntent = new Intent(Intent.ACTION_VIEW);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            installIntent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
            Uri contentUri = FileProvider.getUriForFile(context, BuildConfig.APPLICATION_ID + ".fileprovider", new File(filePath));
            installIntent.setDataAndType(contentUri, "application/vnd.android.package-archive");
        } else {
            installIntent.setDataAndType(Uri.fromFile(new File(filePath)), "application/vnd.android.package-archive");
        }
        PendingIntent updatePendingIntent = PendingIntent.getActivity(context, 0, installIntent, 0);

        Notification.Builder builder = new Notification.Builder(context);
        builder.setContentIntent(updatePendingIntent);
        builder.setSmallIcon(R.mipmap.ic_launcher);// 设置图标
        builder.setWhen(System.currentTimeMillis());// 设置通知来到的时间
        builder.setContentTitle("众屏");// 设置通知的标题
        builder.setContentText("下载完成,点击安装");// 设置通知的内容
        builder.setTicker("下载完成,点击安装");// 状态栏上显示
        builder.setOngoing(true);
        updateNotification = builder.build();

        updateNotification.defaults = Notification.DEFAULT_LIGHTS;// 铃声提醒
        updateNotification.flags = Notification.FLAG_AUTO_CANCEL;


        updateNotificationManager.notify(101, updateNotification);
    }

    public void downLoadFail() {
        Intent nullIntent = new Intent();
        PendingIntent pendingIntent = PendingIntent.getActivity(context, 10, nullIntent, 0);

        Notification.Builder builder = new Notification.Builder(context);
        builder.setContentIntent(pendingIntent);
        builder.setSmallIcon(R.mipmap.ic_launcher);// 设置图标
        builder.setWhen(System.currentTimeMillis());// 设置通知来到的时间
        builder.setContentTitle("众屏");// 设置通知的标题
        builder.setContentText("网络连接不正常，下载失败！");// 设置通知的内容
        builder.setTicker("网络连接不正常，下载失败！");// 状态栏上显示
        builder.setOngoing(true);
        updateNotification = builder.build();

        updateNotification.flags = Notification.FLAG_AUTO_CANCEL;
        updateNotificationManager.notify(101, updateNotification);
    }

    public void alreadyDownLoad(String filePath) {
        updateNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
        Intent installIntent = new Intent(Intent.ACTION_VIEW);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            installIntent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
            Uri contentUri = FileProvider.getUriForFile(context, BuildConfig.APPLICATION_ID + ".fileprovider", new File(filePath));
            installIntent.setDataAndType(contentUri, "application/vnd.android.package-archive");
        } else {
            installIntent.setDataAndType(Uri.fromFile(new File(filePath)), "application/vnd.android.package-archive");
        }
        PendingIntent updatePendingIntent = PendingIntent.getActivity(context, 0, installIntent, 0);

        Notification.Builder builder = new Notification.Builder(context);
        builder.setContentIntent(updatePendingIntent);
        builder.setSmallIcon(R.mipmap.ic_launcher);// 设置图标
        builder.setWhen(System.currentTimeMillis());// 设置通知来到的时间
        builder.setContentTitle("众屏");// 设置通知的标题
        builder.setContentText("下载完成,众屏");// 设置通知的内容
        builder.setTicker("正在更新 众屏！");// 状态栏上显示
        updateNotification = builder.build();

        updateNotification.defaults = Notification.DEFAULT_LIGHTS;// 铃声提醒
        updateNotification.flags = Notification.FLAG_AUTO_CANCEL;
        updateNotificationManager.notify(101, updateNotification);
    }
}
